﻿using System.Web;
using System.Web.Mvc;
using AmazeUI.bll;
using AmazeUI.Imodel;
using Ema.Models;
using Ema.Models.XtModel;
using WebCommon.Extensions;
using WebCommon.Up;

namespace Ema.Controllers
{
    /// <summary>
    /// The base controller.
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// 获取本次信息
        /// </summary>
        /// <returns></returns>
        protected void Logset(string name, string ms, int type = 1)
        {
            var xl = new SubXtLog();
            xl.Remarks = this.Request.UserAgent;
            xl.UsingIp = this.GetIp;
            //获取url，不获取域名。
            xl.Url = Request.RawUrl;
            if (string.IsNullOrWhiteSpace(xl.Url))
            {
                if (Request.Url != null) xl.Url = Request.Url.PathAndQuery;
            }

            xl.Userid = this.GetAdmin.Id;
            xl.Action = "";
            xl.Controllers = "";

            XtLogBll.SetLog(xl, name, ms, type);
        }

        protected string GetHostUrl
        {
            get
            {
                if (System.Web.HttpContext.Current != null)
                {
                    return System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Host;
                    ;
                }
                else
                {
                    return "";
                }

            }
        }

        /// <summary>
        /// 发送json数据
        /// </summary>
        /// <param name="data">
        /// json数据
        /// </param>
        /// <returns>
        /// </returns>
        protected JsonReturn GetJsonDb(object data)
        {
            var jr = new JsonReturn(data);
            return jr;
        }

        /// <summary>
        /// 发送json数据
        /// </summary>
        /// <param name="ms">
        /// 信息，0为正常
        /// </param>
        /// <param name="code">
        /// The code.
        /// </param>
        /// <returns>
        /// </returns>
        [NonAction]
        protected JsonReturn GetJsonDb(string ms, string code = "-1")
        {
            var jr = new JsonReturn();
            jr.CodeMs = ms;
            return jr;
        }

        /// <summary>
        /// 设置获取管理员
        /// </summary>
        protected ILoginModel GetAdmin
        {
            get
            {
                return XtCom.LoginModel;
            }
            set
            {
                XtCom.LoginModel = value;
            }
        }



        /// <summary>
        /// 跳转
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ActionResult Tiaozhuan(string url)
        {
            return this.Redirect(url);
        }

        /// <summary>
        /// 跳转登录
        /// </summary>
        /// <param name="conName"></param>
        /// <param name="url">登陆后返回的url</param>
        /// <returns></returns>
        public ActionResult GoLogin(string conName, string url, string action = "Login")
        {
            return this.RedirectToAction(action, conName, new { url = url });
        }

        /// <summary>
        /// 跳转登录
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ActionResult GoRole(string url)
        {
            return this.RedirectToAction("NotRole", "Admin", new { url = url });
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file">file</param>
        /// <param name="path">路径</param>
        /// <param name="fileName">文件名</param>
        /// <returns></returns>
        [NonAction]
        public bool BaseUp(HttpPostedFileBase file)
        {
            var mapPath = this.Server.MapPath("/");

            var uf = new UploadFile(file, this.Server.MapPath("/"));

            return uf.Save();

        }

        /// <summary>
        /// 获取ip
        /// </summary>
        /// <returns></returns>
        public string GetIp
        {
            get
            {
                HttpRequest request = System.Web.HttpContext.Current.Request;
                string ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(ip))
                {
                    ip = request.ServerVariables["REMOTE_ADDR"];
                }
                if (string.IsNullOrEmpty(ip))
                {
                    ip = request.UserHostAddress;

                }
                if (string.IsNullOrEmpty(ip) || ip.Length > 16)
                {
                    ip = "0.0.0.0";

                } return ip;
            }
        }

    }
}